/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elsimulatorjava;

import java.util.Date;

/**
 *
 * @author corey
 */
public class ElevatorDoorItem {
  /*  private static final int TOP_PANEL_HEIGHT = 40, DOOR_PANEL_WIDTH = 100, SIDE_PANEL_WIDTH = DOOR_PANEL_WIDTH/2,
            DOOR_PANEL_HEIGHT = 2*DOOR_PANEL_WIDTH, TOP_DISPLAY_WIDTH = 120, TEXT_GAP = 5;
    
    private String elevatorText;
    private QRect leftDoor;
    private QRect rightDoor;
    private QRectF boundingRect = new QRectF(0, 0, (2*SIDE_PANEL_WIDTH)+(2*DOOR_PANEL_WIDTH), DOOR_PANEL_HEIGHT+TOP_PANEL_HEIGHT);
    private int elevatorIndex;
    private static final QImage[] number = {new QImage("images/0.jpg","JPG"), new QImage("images/1.jpg","JPG"),
                    new QImage("images/2.jpg","JPG"), new QImage("images/3.jpg","JPG"), new QImage("images/4.jpg","JPG"),
                new QImage("images/5.jpg","JPG"),  new QImage("images/6.jpg","JPG"), new QImage("images/7.jpg","JPG"),
            new QImage("images/8.jpg","JPG"), new QImage("images/9.jpg","JPG")};
    private QImage[] floorNumber = {new QImage(), new QImage()};
    private QImage upImage = new QImage("images/up.jpg", "JPG");
    private QImage downImage = new QImage("images/down.jpg", "JPG");
    private QImage direction = new QImage();
    private Date elapsed;
    
    ElevatorDoorItem(int idx){
        this.elevatorIndex = idx;
        this.elevatorText = "Elevator " + idx;
        leftDoor = new QRect(SIDE_PANEL_WIDTH, TOP_PANEL_HEIGHT, DOOR_PANEL_WIDTH, DOOR_PANEL_HEIGHT);
        rightDoor = new QRect(SIDE_PANEL_WIDTH+DOOR_PANEL_WIDTH, TOP_PANEL_HEIGHT, DOOR_PANEL_WIDTH, DOOR_PANEL_HEIGHT);
        elapsed = new Date();
    }
    
    public void paint(QPainter painter,
                          QStyleOptionGraphicsItem styleOptionGraphicsItem,
                          QWidget widget){
        painter.setBrush(new QBrush(new QImage("images/elTexture.jpg", "JPG")));
        painter.drawRect(leftDoor);
        painter.drawRect(rightDoor);
        painter.setBrush(new QBrush(new QImage("images/elTexture2.jpg", "JPG")));
        painter.drawRect(new QRect(0, TOP_PANEL_HEIGHT, SIDE_PANEL_WIDTH, DOOR_PANEL_HEIGHT));
        painter.drawRect(new QRect((2*DOOR_PANEL_WIDTH)+SIDE_PANEL_WIDTH, TOP_PANEL_HEIGHT, SIDE_PANEL_WIDTH, DOOR_PANEL_HEIGHT));
        painter.setBrush(new QBrush(new QImage("images/elTexture3.jpg", "JPG")));
        painter.drawRect(new QRect(0, 0, 2*(SIDE_PANEL_WIDTH+DOOR_PANEL_WIDTH), TOP_PANEL_HEIGHT));
       // painter.setPen(new QColor(255,0,0));
        //painter.setFont(new QFont());
        
        painter.drawText(0, TOP_PANEL_HEIGHT+DOOR_PANEL_HEIGHT+TEXT_GAP, 2*(SIDE_PANEL_WIDTH+DOOR_PANEL_WIDTH), 20, Qt.AlignmentFlag.AlignCenter.value(), elevatorText);
        painter.setBrush(new QColor(0,0,0));
        painter.drawRect(new QRect(SIDE_PANEL_WIDTH+DOOR_PANEL_WIDTH - (TOP_DISPLAY_WIDTH/2), 5, TOP_DISPLAY_WIDTH, 30));
        painter.drawImage(SIDE_PANEL_WIDTH+DOOR_PANEL_WIDTH+(TOP_DISPLAY_WIDTH/2) - 30, 5, direction);
        painter.drawImage(SIDE_PANEL_WIDTH+DOOR_PANEL_WIDTH+(TOP_DISPLAY_WIDTH/2) - 60, 5, floorNumber[0]);
        painter.drawImage(SIDE_PANEL_WIDTH+DOOR_PANEL_WIDTH+(TOP_DISPLAY_WIDTH/2) - 84, 5, floorNumber[1]);
        
        
    }
    
    public void setDoorClosed(Boolean closeDoors){
        boolean animNotReady = false;
        if (closeDoors){
            if (leftDoor.width() < DOOR_PANEL_WIDTH && rightDoor.width() < DOOR_PANEL_WIDTH){
                leftDoor.setWidth(leftDoor.width()+1);
                rightDoor.setWidth(leftDoor.width()+1);
                animNotReady = true;
            }
        } else{
            if (leftDoor.width() > 0 && rightDoor.width() > 0){
                leftDoor.setWidth(leftDoor.width()-1);
                rightDoor.setWidth(leftDoor.width()-1);
                animNotReady = true;
            }
        }
        this.update();
        //System.out.println(new Date().getTime() - elapsed.getTime());
    }
    
    public boolean isOpenOrClosed(){
        if (leftDoor.width() == 0 || leftDoor.width() == 100){
            return false;
        } else {
            return true;
        }
    }
    
    public QRectF boundingRect() {
            return boundingRect;
    }
    
    int getIndex(){
        return this.elevatorIndex;
    }
    
    public void setFloorNumber(int n){
        String s = String.valueOf(n);
        if (!floorNumber[1].isNull() && n < 10){
            floorNumber[1] = new QImage();
        }
       /* switch (s.length()){
            case 2:
                floorNumber[idx] = number[Integer.parseInt(String.valueOf(s.charAt(s.length()-idx)))];
                idx--;
            case 1:
                floorNumber[idx] = number[Integer.parseInt(String.valueOf(s.charAt(s.length()-idx)))];
        }
        for (int i = 0; i < s.length(); i++){
            floorNumber[i] = number[Integer.parseInt(String.valueOf(s.charAt(s.length() - i - 1)))];
           // pointer--;
        }
        this.update();
    }
    
    public void setDirectionUp(boolean up){
        if (up){
            direction = upImage;
        } else {
            direction = downImage;
        }
        this.update();
    }
    
    public void animateDoors(boolean close){
        if (close){
            
        } else {
            
        }
    }*/
    
}
